package org.lld.im.service.user.controller;

import org.lld.im.common.ClientType;
import org.lld.im.common.ResponseVO;
import org.lld.im.common.route.RouteHandler;
import org.lld.im.common.route.algorithm.RouteInfo;
import org.lld.im.common.utils.RouteInfoParseUtil;
import org.lld.im.service.user.model.req.*;
import org.lld.im.service.user.service.ImUserStatusService;
import org.lld.im.service.user.service.UserService;
import org.lld.im.service.utils.ZKit;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: LunBoWang
 * @description: 用户相关接口
 * @date: 2025/3/13 下午7:55
 * @version: 1.0
 */
@RestController
@RequestMapping("v1/user")
public class ImUserController {
    @Resource
    UserService userService;
    @Resource
    RouteHandler routeHandler;
    @Resource
    ZKit zKit;
    @Resource
    ImUserStatusService imUserStatusService;

    //导入用户资料
    @PostMapping("/importUser")
    public ResponseVO importUser(@RequestBody ImportUserReq req, Integer appId) {
        req.setAppId(appId);
        return userService.importUser(req);
    }

    //删除用户资料
    @DeleteMapping("/deleteUser")
    public ResponseVO deleteUser(@RequestBody @Validated DeleteUserReq req, Integer appId) {
        req.setAppId(appId);
        return userService.deleteUser(req);
    }

    //im登录接口，返回im地址
    @RequestMapping("/login")
    public ResponseVO login(@RequestBody @Validated LoginReq req, Integer appId) {
        req.setAppId(appId);
        ResponseVO login = userService.login(req);
        if (login.isOk()) {
            //去zk获取一个im的地址，返回给sdk
            List<String> allNode = new ArrayList<>();
            if (req.getClientType() == ClientType.WEB.getCode()) {
                allNode = zKit.getAllWebNode();
            } else {
                allNode = zKit.getAllTcpNode();
            }
            //ip:port
            String s = routeHandler.routeServer(allNode, req.getUserId());
            RouteInfo parse = RouteInfoParseUtil.parse(s);
            return ResponseVO.successResponse(parse);
        }
        return ResponseVO.successResponse();
    }

    @RequestMapping("/getUserSequence")
    public ResponseVO getUserSequence(@RequestBody @Validated
                                      GetUserSequenceReq req, Integer appId){
        req.setAppId(appId);
        return userService.getUserSequence(req);
    }

    @RequestMapping("/subscribeUserOnlineStatus")
    public ResponseVO subscribeUserOnlineStatus(@RequestBody @Validated
                                                SubscribeUserOnlineStatusReq req, Integer appId,String identifier){
        req.setAppId(appId);
        req.setOperator(identifier);
        imUserStatusService.subscribeUserOnlineStatus(req);
        return ResponseVO.successResponse();
    }

    @RequestMapping("/setUserCustomerStatus")
    public ResponseVO setUserCustomStatus(@RequestBody @Validated
                                                SetUserCustomerStatusReq req, Integer appId,String identifier){
        req.setAppId(appId);
        req.setOperator(identifier);
        imUserStatusService.setUserCustomerStatus(req);
        return ResponseVO.successResponse();
    }

    @RequestMapping("/queryFriendOnlineStatus")
    public ResponseVO queryFriendOnlineStatus(@RequestBody @Validated
                                          PullFriendOnlineStatusReq req, Integer appId,String identifier){
        req.setAppId(appId);
        req.setOperator(identifier);
        return ResponseVO.successResponse(imUserStatusService.queryFriendOnlineStatus(req));
    }

    @RequestMapping("/queryUserOnlineStatus")
    public ResponseVO queryUserOnlineStatus(@RequestBody @Validated
                                              PullUserOnlineStatusReq req, Integer appId,String identifier){
        req.setAppId(appId);
        req.setOperator(identifier);
        return ResponseVO.successResponse( imUserStatusService.queryUserOnlineStatus(req));
    }
}
